/**
 *******************************************************************************
 * @file system_init.h
 * @author .ISS_AUTO (hudandan@issauto.com)
 * @date 2024-05-15  1. Base Version.
 *       2024-09-04  1. Version: 0.1.0.
 *
 * @brief  This file for the System layer initialization specification.
 *
 * @copyright Copyright (c) 2024 ISSAUTO TECH Co., Ltd. All rights reserved.
 *
 *******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SYS_INIT_H
#define __SYS_INIT_H

#ifdef __cplusplus
extern "C" {
#endif



/* Includes ------------------------------------------------------------------*/
#include "app_config.h"

#include "gpio_driver.h"
#include "flash_driver.h"
#include "adc_driver.h"
#include "timer_driver.h"
#include "uart_driver.h"
#include "rtc_driver.h"
#include "can_driver.h"

#include "system_storage.h"


/** @addtogroup System Driver Layer Module Configuration(Only For Debug)
  * @{
  */
#define LSI_TIM_MEASURE              (0U) //!< LSI校准

#if defined(AK601)
#define LPM_ENABLE                   (0U) //!< 低功耗管理
#define IWDG_ENABLE                  (1U) //!< IWDG使能

#else
#define LPM_ENABLE                   (0U) //!< 低功耗管理
#define IWDG_ENABLE                  (1U) //!< IWDG使能

#endif


/**
  * @}
  */


/* Exported types ------------------------------------------------------------*/
enum {
    SYSCLK_PLLSRC_HSI = 0,
    SYSCLK_PLLSRC_HSE,
};


/* Exported macro ------------------------------------------------------------*/

/** @addtogroup SystemClock configuration
  * @{
  */
#define SYSCLK_SRC_HSI_ENABLE        (0U)
#define SYSCLK_SRC_HSE_ENABLE        (0U)
#define SYSCLK_SRC_PLL_ENABLE        (1U)

/**
  * @}
  */

/** @addtogroup LOW POWER configure
  * @{
  */
#define LOW_POWER_DISABLE     (0U)
#define SLEEP_MODE            (0U)
#define STOP0_MODE            (2U)
#define STOP2_MODE            (3U)
#define STANDBY_MODE          (4U)
#define VBAT_MODE             (5U)

#if (LPM_ENABLE)
#define LOW_POWER_MODE        STOP2_MODE
#else
#define LOW_POWER_MODE        LOW_POWER_DISABLE
#endif

/**
  * @}
  */


/* Exported constants --------------------------------------------------------*/

/** @addtogroup Specifies the Data Backup Register Configuration:
  * @{
  */
//!< 唤醒标志
#define BKP_REG_ID_WKUP_FLAG               BKP_DAT1
#define BKP_DATA_WKUP_FLAG_DEFAULT         0x000F
#define BKP_DATA_WKUP_FLAG_EXIT_PA0        0x0001
#define BKP_DATA_WKUP_FLAG_WDG             0x0002
#define BKP_DATA_WKUP_FLAG_EXIT_PXX        0x0003

#define BKP_DATA_WKUP_FLAG_MASK            0xFF00

//!< 调试打印使能标志
#define BKP_REG_ID_PRINTF_EN               BKP_DAT2

//!< 用户自定义...
#if defined(AK601)

#endif

/**
  * @}
  */


#if (IWDG_ENABLE)
extern __IO uint32_t SYSCLK_LSI_FREQ;
#endif

#if (LSI_TIM_MEASURE)
extern __IO uint16_t IC1ReadValue1, IC1ReadValue2;
extern __IO uint16_t LSI_CaptureNumber;
extern __IO uint32_t Capture;
extern __IO uint32_t C_LsiFreq;
#endif

/* Exported variables --------------------------------------------------------*/
extern RCC_ClocksType sRCC_ClockFreq;

/* Exported functions --------------------------------------------------------*/
extern void i_showUtf8Hex(uint8_t *data, uint32_t len);

/* Exported functions ------------------------------------------------------- */
void User_SystemReset(void);
#if defined (USE_TESSY_TEST)
#define DRV_Error_Handler(error)    return(error)
#define Error_Handler()             return
#else
extern void DRV_Error_Handler(uint32_t errorCode);
extern void Error_Handler(void);
#endif

#if (LPM_ENABLE)
void LPM_FlagClear(void);
void LPM_IntCallback(uint16_t flag);
uint16_t LPM_WkupFlagRead(void);
#else
#define LPM_IntCallback(flag)     USER_PRINTF("+LPM_INT: %04X\r\n", flag)
#endif

#if (LSI_TIM_MEASURE)
void LSI_TIM_Measure(void);
#endif

#if (IWDG_ENABLE)
void IWDG_Configuration(void);
void IWDG_Feed(void);
#endif

/** @example
 * e.g.
 * 1. Enable HSI (Frq = 8000000)
 * SetSysClockToHSI();
 *
 * 2. Enable HSE (Frq = HSE_VALUE)
 * SetSysClockToHSE();
 *
 * 3. Enable HSI-PLL (Frq = 72000000)
 * SetSysClockToPLL(72000000, SYSCLK_PLLSRC_HSI)
 *
 * 4. Enable HSE-PLL (Frq = 144000000)
 * SetSysClockToPLL(144000000, SYSCLK_PLLSRC_HSE)
 */
#if (SYSCLK_SRC_HSI_ENABLE)
int SetSysClockToHSI(void);
#endif
#if (SYSCLK_SRC_HSE_ENABLE)
int SetSysClockToHSE(void);
#endif
#if (SYSCLK_SRC_PLL_ENABLE)
int SetSysClockToPLL(uint32_t freq, uint8_t src);
#endif

void SysClock_Configuration(void);
void SysTick_Configuration(void);
void Backup_Configuration(void);

#if (LPM_ENABLE)
void WakeUpPin_Configuration(void);
void GPIO_LP_Configuration(void);
#endif
void GPIO_Configuration(void);

#if (LPM_ENABLE)
void LOG_LP_Configuration(void);
#endif
void LOG_Configuration(void);

/* Exported functions --------------------------------------------------------*/
void SYS_BOARD_INIT(void);

#if (LPM_ENABLE)
void SYS_DeInit(void);
#endif
void SYS_Init(void);
void SYS_Handle(void *arg);

#ifdef __cplusplus
}
#endif

#endif/* __SYS_INIT_H */

/* END OF FILE ---------------------------------------------------------------*/
